# demo03_dict.py   查询
score = {"yuwen": 80, "shuxue": 75}
print(score, type(score), len(score))  # 2
# 查询  key -> value
# 1. 字典[key]: key不存在则报错
print(score["yuwen"], score["shuxue"])  # 80 75
# print(score["yingyu"])  # KeyError: 'yingyu'
# 2. 字典.get(key, 默认值=None)
print(score.get("yuwen"), score.get("shuxue"))
print(score.get("yingyu", "英语缺考"))  # 英语缺考
# 3. 字典.keys()
print(score.keys())  # dict_keys(['yuwen', 'shuxue'])
for key in score:  # for key in score.keys():
    print("key:", key)
# 4. 字典.values()
print(score.values())  # dict_values([80, 75])
# 5. 字典.items(): 所有的键值对
print(score.items())
# dict_items([('yuwen', 80), ('shuxue', 75)])
# score = {"yuwen": 80, "shuxue": 75}
# 添加/修改
# 1. 字典[key] = value
score["yuwen"] = 90  # key存在则修改
score["yingyu"] = 70  # key不存在则添加
# 2. 字典.update(新字典)
score.update({"shuxue": 85, "tiyu": 100})
# 删除 1. 字典.pop(key):
#        通过key删除元素，并将该key对应的value返回
data = score.pop("yingyu")
# 2. del 元素
del score["tiyu"]
# 3. 字典.clear()
score.clear()
print(score)